package com.yulongtian.oneProblemEveryDay.month202301;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/sentence-similarity-iii/
 * 可
 *
 * @author yulongTian
 * @create 2023-01-16 22:05
 */
public class Test13 {
    public static void main(String[] args) {
        String s2 = "AB";
        String s1 = "AAAB";
        System.out.println(areSentencesSimilar(s1, s2));
    }

    public static boolean areSentencesSimilar(String sentence1, String sentence2) {
        String[] s1 = sentence1.split(" ");
        String[] s2 = sentence2.split(" ");
        //始终保持s1长  s2短
        if (s1.length < s2.length) {
            String[] temp = s1;
            s1 = s2;
            s2 = temp;
        }

        //从左往右遍历s2,寻找s2中不同的开始索引
        int left = -1;
        for (int i = 0; i < s2.length; i++) {
            if (!s1[i].equals(s2[i])) {
                left = i;
                break;
            }
        }
        if (left == -1) {
            left = s2.length;
        }

        //从右往左遍历s2,寻找s2中不同的开始索引
        int right = s2.length;
        for (int i = s2.length - 1, j = s1.length - 1; i >= 0 && j >= 0; i--, j--) {
            if (!s1[j].equals(s2[i])) {
                right = i;
                break;
            }
        }
        if (right == s2.length) {
            right = -1;
        }


        return left - right >= 1;
    }
}
